package com.wb.spring.boot.autoconfigure.model;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author deeprado
 * @Date 2022/8/29 11:22
 */
public class ServerInfo {
    /**
     * 服务Ip
     */
    private String localIp;

    /**
     * 服务端口
     */
    private Integer localPort;

    /**
     * 服务名称
     */
    private String serviceName;

    public String getLocalIp() {
        return localIp;
    }

    public void setLocalIp(String localIp) {
        this.localIp = localIp;
    }

    public Integer getLocalPort() {
        return localPort;
    }

    public void setLocalPort(Integer localPort) {
        this.localPort = localPort;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public ServerInfo(Environment environment) throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        this.localIp = localHost.getHostAddress();
        this.localPort = Integer.valueOf(environment.getProperty("server.port"));
        this.serviceName = environment.getProperty("wb.name");
        if (StringUtils.isBlank(this.serviceName)) {
            throw new NullPointerException("请设置服务名称，wb.name");
        }
    }

    public ServerInfo(String localIp, Integer localPort, String serviceName) {
        this.localIp = localIp;
        this.localPort = localPort;
        this.serviceName = serviceName;
    }
}
